package util

import (
	"io"
	"io/ioutil"
	"net/http"
)

// 1.请求方式 get post ...
// 2.参数- header

// get:

type CurlInterface interface {
	Get() (string, error)
}

type Curl struct {
	method string
	api    string
	body   io.Reader
	Header http.Header
}

func NewCurl(api string, body io.Reader) CurlInterface {
	return &Curl{
		api:  api,
		body: body,
	}
}

func (c *Curl) Get() (string, error) {
	// 获取request请求
	req, err := http.NewRequest("GET", c.api, c.body)

	if err != nil {
		return "", err
	}
	// client利用request去发送请求
	client := http.Client{}
	rsp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer rsp.Body.Close()

	// 处理消息
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		return "", err
	}
	return string(body), nil
}
